package health.lm.com.find.activity;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.androidkun.xtablayout.XTabLayout;
import com.jakewharton.rxbinding2.view.RxView;
import com.vondear.rxtool.RxDeviceTool;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import health.lm.com.component_base.base.adapter.BaseFragmentAdapter;
import health.lm.com.component_base.base.mvp.activity.XActivity;
import health.lm.com.component_base.helper.ImageLoaderHelper;
import health.lm.com.component_base.widget.RatingBar;
import health.lm.com.find.R;
import health.lm.com.find.R2;
import health.lm.com.find.entity.MerchantsEntity;
import health.lm.com.find.fragment.EvaluationFragment;
import health.lm.com.find.fragment.IntroductionFragment;
import health.lm.com.find.presenter.MerchantsPresenter;
import health.lm.com.router.ARouterConstant;
import io.reactivex.functions.Consumer;

/**
 * @author GQI
 * @description: 联盟商详情
 * @date :2019-08-14 17:41
 */
@Route(path = ARouterConstant.MerchantsActivity)
public class MerchantsActivity extends XActivity<MerchantsPresenter> {
    @BindView(R2.id.detail_toolbar)
    Toolbar detailToolbar;
    @BindView(R2.id.toolbar_layout)
    CollapsingToolbarLayout toolbarLayout;
    @BindView(R2.id.app_bar)
    AppBarLayout appBar;
    @BindView(R2.id.btn_into)
    Button btnInto;
    @BindView(R2.id.tv_experience_top)
    TextView tvExperienceTop;
    @BindView(R2.id.rb_start)
    RatingBar rbStart;
    @BindView(R2.id.tv_experience_center)
    TextView tvExperienceCenter;
    @BindView(R2.id.tv_experience_bottom)
    TextView tvExperienceBottom;
    @BindView(R2.id.iv_experience_bottom)
    ImageView ivExperienceBottom;
    @BindView(R2.id.iv_background)
    ImageView ivBackground;
    @BindView(R2.id.xTablayout)
    XTabLayout xTablayout;
    @BindView(R2.id.viewPager)
    ViewPager viewPager;
    @Autowired
    String id = "";
    List<Fragment> fragments = new ArrayList<>();
    private MerchantsEntity entity;


    @Override
    public int getLayoutId() {
        return R.layout.activity_merchants;
    }

    @Override
    public MerchantsPresenter newP() {
        return new MerchantsPresenter();
    }

    @SuppressLint("CheckResult")
    @Override
    public void initData(Bundle savedInstanceState) {
//        setSupportActionBar(detailToolbar);
//        ActionBar actionBar = getSupportActionBar();
//        if (actionBar != null) {
//            actionBar.setDisplayHomeAsUpEnabled(true);
//        }
        detailToolbar.setNavigationOnClickListener(v -> finish());
        RxView.clicks(btnInto)
                .compose(bindToLife())
                .throttleFirst(2, TimeUnit.SECONDS)
                .subscribe(new Consumer<Object>() {
                    @Override
                    public void accept(Object o) throws Exception {
                        gotoActivity(ARouterConstant.ShopEvaluationActivity);
                    }
                });
        RxView.clicks(tvExperienceCenter)
                .throttleFirst(2,TimeUnit.SECONDS)
                .compose(bindToLife())
                .compose(rxPermissions.ensure(Manifest.permission.CALL_PHONE))
                .subscribe(aBoolean -> {
                    if (aBoolean){
                        RxDeviceTool.callPhone(context,entity.getTel());
                    }else {
                        getvDelegate().toastShort("请开启相关权限");
                    }
                });

        getP().getData(id);
    }

    @Override
    protected void setImmersionBar() {
        mImmersionBar.titleBar(detailToolbar).statusBarDarkFont(true).init();
    }

    public void getDataSuccess(MerchantsEntity entity){
        this.entity = entity;
        if (this.entity == null){
            return;
        }
        //我要评价按钮
        btnInto.setVisibility(View.GONE);
        //商家名称
        tvExperienceTop.setText(entity.getTitle());
        //星级评价
        rbStart.setStar(4.5f);
        //电话号码
        tvExperienceCenter.setText(String.format("电话：%s",entity.getTel()));
        //地址
        tvExperienceBottom.setText(String.format("地址：%s",entity.getAddress()));
        //背景图
        ImageLoaderHelper.getInstance().load(mActivity,entity.getBackground(),ivBackground);
        initViewPager();
    }
    private void initViewPager() {
        List<String> titles = new ArrayList<>();
        titles.add("简介");
        titles.add("评价");
        fragments.add( IntroductionFragment.getInstance(entity.getDetail()));
        fragments.add( EvaluationFragment.getInstance(id));
        BaseFragmentAdapter adatper = new BaseFragmentAdapter(getSupportFragmentManager(), fragments, titles);
        viewPager.setAdapter(adatper);
        viewPager.setOffscreenPageLimit(2);
        //将TabLayout和ViewPager关联起来。
        xTablayout.setupWithViewPager(viewPager);
        //给TabLayout设置适配器
        xTablayout.setupWithViewPager(viewPager);

    }
    /**
     * 拨打电话
     */
    public void dialPhoneNumber(String phoneNumber) {
        Intent intent = new Intent(Intent.ACTION_DIAL);
        intent.setData(Uri.parse("tel:" + phoneNumber));
        if (intent.resolveActivity(mActivity.getPackageManager()) != null) {
            startActivity(intent);
        }
    }
}
